package com.example.springbootvuetest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springbootvuetest.entity.IdleItem;
import com.example.springbootvuetest.entity.Message;
import com.example.springbootvuetest.entity.User;
import com.example.springbootvuetest.mapper.IdleItemMapper;
import com.example.springbootvuetest.mapper.MessageMapper;
import com.example.springbootvuetest.mapper.UserMapper;
import com.example.springbootvuetest.service.IMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shuoye
 * @since 2023-03-14
 */
@Service

public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IdleItemMapper idleItemMapper;

    /**
     * 查询一个闲置下的所有留言，未做分页
     * 同时查出发送者和接收者的信息
     * idleId建索引
     *
     * @param idleId
     * @return
     */
    public List<Message> getAllIdleMessage(Integer idleId) {

        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("idle_id", idleId).orderByDesc("create_time");
        List<Message> list = messageMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            List<Integer> idList = new ArrayList<>();
            for (Message i : list) {
                idList.add(i.getUserId());
            }
            List<User> userList = userMapper.selectBatchIds(idList);
            Map<Integer, User> map = new HashMap<>();
            for (User user : userList) {
                map.put(user.getId(), user);
            }
            for (Message i : list) {
                i.setFromU(map.get(i.getUserId()));
            }
            Map<Integer, Message> mesMap = new HashMap<>();
            for (Message i : list) {
                mesMap.put(i.getId(), i);
            }
            for (Message i : list) {
                Message toM = new Message();
                User toU = new User();
                if (i.getToMessage() != null) {
                    toM.setContent(mesMap.get(i.getToMessage()).getContent());
                    toU.setNickname(map.get(i.getToUser()).getNickname());
                }
                i.setToM(toM);
                i.setToU(toU);
            }
        }
        return list;
    }

    /**
     * 获取每一条消息总物品对应的人和物品
     *
     * @param userId
     * @return
     */
    @Override
    public List<Message> getAllMyMessage(Integer userId) {
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).orderByDesc("create_time");
        List<Message> list = messageMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            List<Integer> idList = new ArrayList<>();
            for (Message i : list) {
                idList.add(i.getUserId());
            }
            List<User> userList = userMapper.selectBatchIds(idList);
            Map<Integer, User> map = new HashMap<>();
            for (User user : userList) {
                map.put(user.getId(), user);
            }
            for (Message i : list) {
                i.setFromU(map.get(i.getUserId()));
            }

            List<Integer> idleIdList = new ArrayList<>();
            for (Message i : list) {
                idleIdList.add(i.getIdleId());
            }
            List<IdleItem> idleList = idleItemMapper.selectBatchIds(idleIdList);
            Map<Integer, IdleItem> idleMap = new HashMap<>();
            for (IdleItem idle : idleList) {
                idleMap.put(idle.getId(), idle);
            }
            for (Message i : list) {
                i.setIdle(idleMap.get(i.getIdleId()));
            }
        }
        return list;
    }
}
